package com.zyf.ui;


import com.zyf.utils.ICommons;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Map;

public class SelectTypeWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    private static JLabel label = new JLabel();
    private String serviceType = "";
    private JTextField srcfileField = new JTextField();
    private JTextField txtpathField = new JTextField();
    private JTextField autorField = new JTextField();
    private JComboBox actionType = new JComboBox();// 业务选择
    private JButton pathBtn = new JButton();// ѡ��ť
    private JButton pathBtnsrc = new JButton();// ѡ��ť
    private Map<String, Object> result;

    private static final int WIDTH=300;
    private static final int HEIGHT=25;

    private static final int LABELWIDTH=260;

    private static final int LABELSTARTX=30;
    private static final int SCREENWIDTH=650;

    public SelectTypeWindow() {
        int startX = 330;
        int startY = 300;
        int startWidth = 150;
        int tipLabelWidth = 600;
        int typeStartY = 35;

        setTitle("业务选择框");
        setSize(SCREENWIDTH, 500);
        setDefaultCloseOperation(3);
        setLocationRelativeTo(null);
        setLayout(null);

        JLabel jla = new JLabel("请选择要进行的业务:");
        jla.setFont(new Font("楷体", Font.BOLD, HEIGHT));
        jla.setBounds(LABELSTARTX, typeStartY, LABELWIDTH, HEIGHT);
        jla.setBackground(Color.MAGENTA);
        add(jla);
        //
        actionType.setBounds(LABELSTARTX + LABELWIDTH, typeStartY, WIDTH, HEIGHT+5);
        actionType.setFont(new Font("楷体", Font.BOLD, HEIGHT));
        add(actionType);

        JButton jbu = new JButton("下一步");
        jbu.setFont(new Font(ICommons.TYPEFACE, 1, 28));
        Color color = new Color(28, 144, 62, 255);
        jbu.setForeground(color);
        jbu.addActionListener(new ButtonListener(jbu));
        jbu.setBounds((SCREENWIDTH-startWidth)/2, startY, startWidth, 40);
        add(jbu);

        // 业务选择窗
        createServiceChooser();
        // 文件选择框
        createFilechooser();

        label.setBounds((SCREENWIDTH-tipLabelWidth)/2, startY + 60, tipLabelWidth, 60);
        label.setFont(new Font(ICommons.TYPEFACE, 1, 30));
        label.setForeground(Color.RED);
        add(label);

        //设置背景图片,创建一个JLabel并设置图片
        InputStream inputstream = this.getClass().getClassLoader().getResourceAsStream("back.jpg");
        ByteArrayOutputStream byteArrayInputStream =null;
        try {
            // 创建ByteArrayOutputStream
            byteArrayInputStream = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[1024]; // 缓冲区大小为1024字节
            // 读取InputStream并写入ByteArrayOutputStream
            while ((nRead = inputstream.read(data, 0, data.length)) != -1) {
                byteArrayInputStream.write(data, 0, nRead);
            }
            // 将收集的数据转换为字节数组
            byte[] byteArray = byteArrayInputStream.toByteArray();
            JLabel background = new JLabel(new ImageIcon(byteArray));
            background.setOpaque(true); // 设置为不透明
            background.setLayout(null); // 设置布局管理器为null
            // 设置JLabel的大小与JFrame相同
            background.setBounds(0, 0, this.getWidth(), this.getHeight());
            this.add(background);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputstream.close();
                byteArrayInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        setVisible(true);
    }


    private void createServiceChooser() {
        InputStream inputstream = this.getClass().getClassLoader().getResourceAsStream("configuration.txt");
        BufferedReader reader = null;
        String temp = "";
        try {
            reader = new BufferedReader(new InputStreamReader(inputstream));
            while ((temp = reader.readLine()) != null) {
                if (temp.startsWith("actiontype")) {
                    this.actionType.addItem(temp.substring(temp.indexOf("=") + 1).trim());
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();

            if (reader != null)
                try {
                    reader.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void createFilechooser() {
        int y = 80;
        int interval = 40;
        String txt = "";
        for (int i = 0; i < 2; i++) {
            JLabel label = null;
            if (i == 0) {
                srcfileField.setBounds(LABELSTARTX + LABELWIDTH , y + interval * i, WIDTH, HEIGHT);
                txt = "请输入用户名:";
                label = new JLabel(txt);// 标签
            }
            if (i == 1) {
                txtpathField.setBounds(LABELSTARTX + LABELWIDTH , y + interval * i, WIDTH, HEIGHT);
                txt = "请输入密码:";
                label = new JLabel(txt);// 标签
            }
            if (label != null) {
                label.setFont(new Font("楷体", Font.BOLD, HEIGHT));
                label.setBounds(LABELSTARTX, y + interval * i, LABELWIDTH, HEIGHT);
                this.add(label);
            }

            this.add(srcfileField);
            this.add(txtpathField);
        }

    }


    /**
     * ��ť������
     *
     * @author Lenovo
     */
    class ButtonListener implements ActionListener {
        private JButton button;

        public ButtonListener(JButton button) {
            this.button = button;
        }

        public void actionPerformed(ActionEvent e) {
            label.setVisible(false);
            label.setHorizontalAlignment(SwingConstants.CENTER);
//            if (StringUtil.isBlank(srcfileField.getText())) {
//                label.setText("用户名输入有误!");
//                label.setVisible(true);
//                return;
//            }
//            if (StringUtil.isBlank(txtpathField.getText())) {
//                label.setText("密码输入有误!");
//                label.setVisible(true);
//                return;
//            }
            try {
                int windowType= actionType.getSelectedIndex()+1;
                DrawingWindowService.createUI(windowType);
            } catch (Exception ex) {
                label.setText(ex.toString());
                label.setVisible(true);
            }
        }
    }


}